//
//  PickerImageAddPhotoCell.m
//  AwesomeRecorder
//
//  Created by Xing on 2019/11/29.
//  Copyright © 2019 Xing. All rights reserved.
//

#import "PickerVideoAddPhotoCell.h"
#import "ARButton.h"

@interface PickerVideoAddPhotoCell()

@property(nonatomic,strong) UIView *bgView;
@property(nonatomic,strong) UIImageView *photoIcon;
@property(nonatomic,strong) UIImageView *playIcon;
@property(nonatomic,strong) ARButton *removeBtn;

@property(nonatomic,strong) UIView *progressView;
@property(nonatomic,strong) UIView *progressBar;
@property(nonatomic,strong) UILabel *progressLabel;

@end

@implementation PickerVideoAddPhotoCell

- (instancetype)initWithFrame:(CGRect)frame {
    
    self = [super initWithFrame:frame];
    
    if (self) {
        [self addView];
    }
    
    return self;
}

- (void) addView {
    
    _bgView = [[UIView alloc] initWithFrame:self.bounds];
    _bgView.clipsToBounds = YES;
    [Utils resetView:_bgView bgColor:[UIColor clearColor] cornerRadius:_cornerRadius borderWidth:0 borderColor:nil];
    [self addSubview:_bgView];
    
    _photoIcon = [[UIImageView alloc] initWithFrame:_bgView.bounds];
    _photoIcon.backgroundColor = RGB(0.95, 0.95, 0.95);
    _photoIcon.clipsToBounds = YES;
    _photoIcon.contentMode = UIViewContentModeScaleAspectFit;
    [_bgView addSubview:_photoIcon];
    
    _playIcon = [[UIImageView alloc] initWithFrame:CGRectMake((_bgView.width-24)/2, (_bgView.height-24)/2, 24, 24)];
    _playIcon.image = [UIImage imageNamed:@"play_start_icon.png"];
    [_bgView addSubview:_playIcon];
    
    _removeBtn = [ARButton buttonWithFrame:CGRectMake(_photoIcon.width-30, 0, 30, 30) imageRect:CGRectMake(5+_removeIconOffset.x, _removeIconOffset.y, 25, 25)];
    [_removeBtn addTarget:self action:@selector(closeAction:) forControlEvents:UIControlEventTouchUpInside];
    _removeBtn.exclusiveTouch = YES;
    [self addSubview:_removeBtn];
}

- (void) layoutSubviews {
    
    [super layoutSubviews];
}

- (void)setCornerRadius:(CGFloat)cornerRadius {
    
    _cornerRadius = cornerRadius;
    
    if (_bgView) {
        [Utils resetView:_bgView bgColor:nil cornerRadius:_cornerRadius borderWidth:0 borderColor:nil];
    }
}

- (void)setRemoveIconOffset:(CGPoint)removeIconOffset {
    
    _removeIconOffset = removeIconOffset;
    
    if (_removeBtn) {
        _removeBtn.imageRect = CGRectMake(5+_removeIconOffset.x, _removeIconOffset.y, 25, 25);
    }
}

- (void) closeAction:(ARButton *)sender {
    
    if (self.cellDidClickCloseAction) {
        
        self.cellDidClickCloseAction(_index);
    }
}

- (void) setRemoveIcon:(UIImage *)removeIcon {
    
    _removeIcon = removeIcon;
    
    [_removeBtn setImage:removeIcon forState:UIControlStateNormal];
}

- (void) refreshWithVideoObject:(PickerVideo *)video
                isAddIcon:(BOOL)isAddIcon
                 editable:(BOOL)editable
                    index:(NSInteger)index {
    
    _index = index;
    _videoObject = video;
    _editable = editable;
    
    if (isAddIcon) {
        _photoIcon.image = self.addIcon;
        _photoIcon.backgroundColor = RGB(0.95, 0.95, 0.95);
        _removeBtn.hidden = YES;
        _progressView.hidden = YES;
        _playIcon.hidden = YES;
    } else {
        _photoIcon.image = video.coverImage;
        _photoIcon.backgroundColor = [UIColor blackColor];
        _removeBtn.hidden = !editable;
        _playIcon.hidden = NO;
    }
}

- (void) dealloc {
    
    _addIcon = nil;
    _removeIcon = nil;
}

@end
